package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.Student;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import jdk.internal.org.objectweb.asm.tree.TableSwitchInsnNode;

/**
 * @date: 2022-08-03 10:55
 * @author: ztkj-hzb
 * @description: tableview 提示框 设置
 **/
public class TableViewTipsSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();


        ObservableList<Student> persons = FXCollections.observableArrayList();
        persons.add(new Student("小天", "男", 0.25, false));
        persons.add(new Student("小地", "女", 0.5, Boolean.TRUE));
        persons.add(new Student("小玄", "男", 0.75, false));
        persons.add(new Student("小黄", "女", 1.0, true));

        TableView<Student> tableView = new TableView<>(persons);

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        tableView.setPrefSize(400, 400);


        TableColumn<Student, String> nameTableCol = new TableColumn<>("学生名称");
        TableColumn<Student, String> sexTableCol = new TableColumn<>("性别");
        TableColumn<Student, Double> scoreTableCol = new TableColumn<>("成绩");
        TableColumn<Student, Boolean> passTableCol = new TableColumn<>("是否及格");

        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        sexTableCol.setCellValueFactory(new PropertyValueFactory<>("sex"));
        scoreTableCol.setCellValueFactory(new PropertyValueFactory<>("score"));
        passTableCol.setCellValueFactory(param -> param.getValue().getPassProperty());


        //TODO 设置整行根据条件设置边框,需要设置在show之前
        tableView.setRowFactory(new Callback<TableView<Student>, TableRow<Student>>() {
            @Override
            public TableRow<Student> call(TableView<Student> param) {
                return new TableRow<Student>() {
                    /**
                     *
                     */
                    @Override
                    protected void updateItem(Student item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            return;
                        }

                        //成绩不及格的标记颜色
                        if (!item.getPass()) {
                            this.setStyle("-fx-background-color: pink");
                        }

                        //添加提示框
                        this.setTooltip(new Tooltip(String.format("成绩：%s", item.getPass() ? "及格" : "不及格")));

                    }
                };
            }
        });


        //TODO 指定列设置提示
        scoreTableCol.setCellFactory(new Callback<TableColumn<Student, Double>, TableCell<Student, Double>>() {
            @Override
            public TableCell<Student, Double> call(TableColumn<Student, Double> param) {
                return new TableCell<Student, Double>() {
                    /**
                     *
                     */
                    @Override
                    protected void updateItem(Double item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            return;
                        }

                        boolean isPass = true;
                        if (item < 0.75) {
                            isPass = false;
                            this.setStyle("-fx-background-color: #F0FFF0");
                        }

                        this.setTooltip(new Tooltip(String.format("成绩：%s--%s", item, isPass ? "合格" : "不合格")));

                        this.setGraphic(new Label(item.toString()));
                    }
                };
            }
        });


        tableView.getColumns().addAll(nameTableCol, sexTableCol, scoreTableCol, passTableCol);
        anchorPane.getChildren().addAll(tableView);
        AnchorPane.setTopAnchor(tableView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(700);
        primaryStage.show();


    }
}
